//
//  BRCardSetView.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/10/13.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRCardSetView.h"

// 148
#define kSetView_h 96

@interface BRCardSetView ()
{
    UIView *_contentView;
    UIView *_lineView;
    UIImageView *_defaultImg;
    UIImageView *_removeImg;
    QMUILabel *_cardNameL;
    CGFloat _cont_h;
}
@end

@implementation BRCardSetView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _cont_h = kSetView_h;
        [self setUI];
    }
    return self;
}

#pragma mark - ACTION --
- (void)close:(QMUIButton *)btn {
    [self hidd];
}
- (void)bottomAction:(QMUIButton *)btn {
    NSInteger tag = btn.tag;
    if (tag == 200) {
        self.isOpen = NO;
    }else {
        if (self.delegate && [self.delegate respondsToSelector:@selector(cardDelete:)]) {
            [self.delegate cardDelete:self.index];
        }
    }
}

- (void)itemClik:(UITapGestureRecognizer *)tap {
    
//    NSInteger tag = tap.view.tag;
//    if (tag == 100) {
////        self.isDefault = !self.isDefault;
//        if (self.delegate && [self.delegate respondsToSelector:@selector(cardSetDefault:index:)]) {
//            [self.delegate cardSetDefault:self.isDefault index:self.index];
//        }
//    }else {
        self.isOpen = !self.isOpen;
//    }
}

- (void)show {
    self.hidden = NO;
}

- (void)hidd {
    self.hidden = YES;
    self.isOpen = NO;
}

- (void)setIsDefault:(BOOL)isDefault {
    _isDefault = isDefault;
    UIImage *selImg = UIImage.qd_cardDefauImg;
    _defaultImg.image = isDefault == YES ? selImg : UIImageMake(@"card_unselect44");
}

- (void)setTitleText:(NSString *)titleText {
    _titleText = titleText;
    _cardNameL.text = titleText;
}

- (void)setIsOpen:(BOOL)isOpen {
    _isOpen = isOpen;
    _lineView.hidden = !isOpen;
    if (isOpen) {
        _cont_h = 52 + kSetView_h;
        [UIView animateWithDuration:0.25 animations:^{
            
            self->_removeImg.transform = CGAffineTransformMakeRotation(M_PI_2);
            self->_contentView.mj_h = self->_cont_h;
        } completion:^(BOOL finished) {
            
        }];
        
    }else {
        _cont_h = kSetView_h;
        [UIView animateWithDuration:0.25 animations:^{
            self->_removeImg.transform = CGAffineTransformMakeRotation(0);
            self->_contentView.mj_h = self->_cont_h;;
        } completion:^(BOOL finished) {
            
        }];
    }
}

#pragma mark - set ui --
- (void)setUI {
    
    self.backgroundColor = UIColorShadow;
    self.hidden = YES;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 56, _cont_h)];
    contentView.backgroundColor = UIColor.qd_backgroundColor2;
    [self addSubview:contentView];
    contentView.center = self.center;
    _contentView = contentView;
    
    QMUIButton *closeBtn = [[QMUIButton  alloc] init];
    
    [closeBtn setBackgroundImage:UIImage.qd_cardCloseImg forState:UIControlStateNormal];
    closeBtn.titleLabel.font = [UIFont br_fontB:14];
    [contentView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
        make.top.equalTo(contentView);
        make.right.equalTo(contentView).offset(0);
    }];
    [closeBtn addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
    
    QMUILabel *cardNameL = [[QMUILabel alloc] init];
    cardNameL.text = @"--";
    cardNameL.textColor = UIColor.qd_titleTextColor;
    cardNameL.font = [UIFont br_fontB:17];
    cardNameL.textAlignment = NSTextAlignmentLeft;
    [contentView addSubview:cardNameL];
    [cardNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(contentView);
        make.centerY.equalTo(closeBtn).offset(0);
    }];
    _cardNameL = cardNameL;
    
    NSArray *texts = @[Localized(@"wallet_removeCard")];
    NSArray *imgs = @[@"cards_ic_del"];
//    NSArray *texts = @[Localized(@"wallet_setDefaultText"),Localized(@"wallet_removeCard")];UIImageMake(@"cards_ic_setDefault") : UIImageMake(@"cards_ic_del")
    for (int i = 0; i < texts.count; i++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 44 + (52 * i), kScreenWidth - 56, 52)];
        [contentView addSubview:view];
        view.userInteractionEnabled = YES;
        view.tag = 100 + i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemClik:)];
        [view addGestureRecognizer:tap];
        
        UIView *imgBG = [[UIView alloc] initWithFrame:CGRectMake(16, 12, 28, 28)];
        imgBG.backgroundColor =  UIColor.qd_tintColor8;
        imgBG.layer.cornerRadius = 14;
        imgBG.layer.masksToBounds = YES;
        [view addSubview:imgBG];
        
        UIImageView *leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
        leftImg.image = UIImageMake(imgs[i]);
        [imgBG addSubview:leftImg];
        
        QMUILabel *symbolLB = [[QMUILabel alloc] init];
        symbolLB.text = texts[i];
        symbolLB.textColor = i == 0 ? UIColor.qd_titleTextColor2 : UIColorMake(253, 88, 73);
        symbolLB.font = [UIFont br_fontM:14];
        symbolLB.textAlignment = NSTextAlignmentLeft;
        symbolLB.numberOfLines = 2;
        [view addSubview:symbolLB];
        [symbolLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(leftImg.mas_right).offset(8);
            make.right.equalTo(view.mas_right).offset(-50);
        }];
        
        UIImageView *rightImg = [[UIImageView alloc] initWithFrame:CGRectMake(contentView.mj_w - 44, 4, 44, 44)];
        //payment_selectIconSel
//        rightImg.image = i == 0 ? UIImageMake(@"card_unselect44") : UIImageMake(@"arrow_right_gray44");
//        [view addSubview:rightImg];
//        if (i == 0) {
//            _defaultImg = rightImg;
//        }else {
//            _removeImg = rightImg;
//        }
        rightImg.image = UIImageMake(@"arrow_right_gray44");
        [view addSubview:rightImg];
        if (i == 0) {
            _removeImg = rightImg;
        }
    }
    
    for (int i = 0; i < 2; i ++) {
        
        QMUIButton *btn = [[QMUIButton  alloc] init];
        NSString *text = i == 0 ? Localized(@"wallet_cancel") : Localized(@"wallet_remove");
        [btn setTitle:text forState:UIControlStateNormal];
        [btn setTitleColor:kColorMain forState:UIControlStateNormal];
        btn.titleLabel.font = i == 0 ? [UIFont br_fontM:15]: [UIFont br_fontB:15];
        [contentView addSubview:btn];
        btn.frame = CGRectMake((contentView.mj_w / 2.0) * i, kSetView_h, (contentView.mj_w / 2), 52);
        btn.tag = 200 + i;
        [btn addTarget:self action:@selector(bottomAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    for (int i = 0; i < 3; i ++) {
        CGFloat line_h = 0.5;
        CGFloat line_x = 16.;
        CGFloat line_y = 44 + 52.0 * i;
        if (i == 0) {
            line_h = 1.0;
            line_x = 0;
            line_y = 44;
        }
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(line_x, line_y, contentView.mj_w - line_x, line_h)];
        line.backgroundColor = UIColor.qd_lineColor;
        [contentView addSubview:line];
        if (i == 2) {
            _lineView = line;
            line.hidden = YES;
        }
    }
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(contentView.mj_w / 2.0, kSetView_h + 11, 0.5, 30)];
    lineV.backgroundColor = UIColor.qd_lineColor;
    [contentView addSubview:lineV];
    
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    _contentView.layer.cornerRadius = kCorners;
    _contentView.clipsToBounds = YES;
}


@end
